package com.example.match.camera;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;

import com.example.match.mui.MainUIActivity;
import com.example.mylibrary.MyApp;
import com.example.mylibrary.config.Config;
import com.example.mylibrary.util.BitmapUtil;
import com.example.mylibrary.util.EventUtil;

import cn.com.aratek.facelib.AraErrorCode;
import cn.com.aratek.facelib.AraFaceAuthLib;



/**
 * Created by Administrator on 2018/1/24.
 */

public class FaceDetectThread extends Thread {
    private Context context;
    private Handler handler;
    private byte[] NV21data;
    private AraFaceAuthLib araFaceAuthLib;
    private FaceResult faceResult;
    private MyApp app;

    public FaceDetectThread(Context context, Handler handler, byte[] NV21data) {
        this.context = context;
        this.handler = handler;
        this.NV21data = NV21data;
        //双apk运行注视掉一个
     //   araFaceAuthLib = MainMenu.getAraFaceAuthLib();

       araFaceAuthLib = MainUIActivity.getAraFaceAuthLib();
        app = MyApp.getInstance();
    }

    @Override
    public void run() {
        super.run();
        byte[] snapshotData = null;
        Bitmap faceBitmap = null;
        faceBitmap = BitmapUtil.NV21ToBitmap(NV21data, Config.WIDTH,
                Config.HEIGHT, snapshotData, Config.displayOrientation, false);
        int result = -1;
        try {
            result = araFaceAuthLib.FaceDetect(faceBitmap);
        } catch (Exception e) {
            Message m = handler.obtainMessage();
            m.what = EventUtil.UPDATE_NO_FACE_RECT;
            m.sendToTarget();

            return;
        }

        if (result == AraErrorCode.ARA_OK) {
            faceResult = new FaceResult(araFaceAuthLib.getObjRect().iTop,
                    araFaceAuthLib.getObjRect().iBottom,
                    araFaceAuthLib.getObjRect().iLeft,
                    araFaceAuthLib.getObjRect().iRight, faceBitmap);
            Message m = handler.obtainMessage();
            m.what = EventUtil.UPDATE_FACE_RECT;
            m.obj = faceResult;
            m.sendToTarget();
        } else {
            Message m = handler.obtainMessage();
            m.what = EventUtil.UPDATE_NO_FACE_RECT;
            m.sendToTarget();
        }
    }
}
